#include <stdio.h>
#define ROW 3
#define COLUMN 3
#define TRUE 1
#define FALSE 0

void InitBoard(char (*Board)[COLUMN])
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLUMN; j++)
        {
            Board[i][j] = ' ';
        }
    }
}
void PrintBoard(char (*Board)[COLUMN])
{
    printf("\n");
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLUMN; j++)
        {
            printf(" %c ", Board[i][j]);
            if (j < COLUMN - 1)
                printf("|");
        }
        printf("\n");
        if (i < ROW - 1)
            printf("---|---|---\n");
    }
}
int MakeMove(char (*Board)[COLUMN], int row, int column, char player)
{
    if (row < 0 || row >= ROW || column < 0 || column >= COLUMN)
    {
        printf("invalid place!please try again!");
        return FALSE;
    }
    if (Board[row][column] != ' ')
    {
        printf("this place have no place!try again!\n");
        return FALSE;
    }
    Board[row][column] = player;
    return TRUE;
}
int Judge(char player, char (*Board)[COLUMN])
{

    for (int i = 0; i < ROW; i++)
    {
        if (Board[i][0] == player && Board[i][1] == player && Board[i][2] == player)
            return TRUE;
    }
    for (int i = 0; i < COLUMN; i++)
    {
        if (Board[0][i] == player && Board[1][i] == player && Board[2][i] == player)
            return TRUE;
    }
    if (Board[0][0] == player && Board[1][1] == player && Board[2][2] == player)
        return TRUE;
    if (Board[0][2] == player && Board[1][1] == player && Board[2][0] == player)
        return TRUE;
}
int CheckIsDraw(char (*Board)[COLUMN])
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLUMN; j++)
        {
            if (Board[i][j] == ' ')
                return FALSE;
        }
    }
    return TRUE;
}
int main()
{
    char Board[ROW][COLUMN] = {0};
    InitBoard(Board);
    // PrintBoard(Board);

    char player = 'X';
    while (1)
    {
        PrintBoard(Board);
        int row = 0, column = 0;
        printf("please input a place to make move:");
        scanf("%d %d", &row, &column);
        if (MakeMove(Board, row - 1, column - 1, player) == FALSE)
        {
            continue;
        }
        if (Judge(player, Board) == TRUE)
        {
            PrintBoard(Board);
            printf("player %c wins!Congratulations!\n", player);
            break;
        }
        if(CheckIsDraw(Board))
        {
            PrintBoard(Board);
            printf("Draw!Game Over!\n");
            break;
        }
        player = (player == 'X') ? ('O') : ('X');
    }
    return 0;
}